package com.example.newlist;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public ExpandableListView expandableListView;

    public List<String> group_list = new ArrayList<>();
    public List<String> item_lt = new ArrayList<>();
    public List<List<String>> item_list;
    //组布局与子项布局
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);

        //组数据
        group_list = new ArrayList<String>();
        group_list.add("我的好友");
        group_list.add("我的家人");
        group_list.add("兄弟姐妹");
        group_list.add("我的同学");

        //一个组中的子数据
        item_lt = new ArrayList<String>();
        item_lt.add("张三丰");
        item_lt.add("董存瑞");
        item_lt.add("李大钊");

        //将每组的子数据添加到一个数组中
        item_list = new ArrayList<List<String>>();
        item_list.add(item_lt);
        item_list.add(item_lt);
        item_list.add(item_lt);
        item_list.add(item_lt);

        //取消指示器
        expandableListView.setGroupIndicator(null);

        //设置适配器
        expandableListView.setAdapter(new MyExpandableListViewAdapter(this));
        //默认打开
//        for(int i = 0; i < new MyExpandableListViewAdapter(this).getGroupCount(); i++){
//
//            expandableListView.expandGroup(i);
//
//        }

        // 监听组点击
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
        {
            @SuppressLint("NewApi")
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
            {
                if (item_list.get(groupPosition).isEmpty())
                {
                    return true;
                }
                return false;
            }
        });

        // 监听每个分组里子控件的点击事件
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener()
        {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
            {

                Toast.makeText(MainActivity.this, "group=" + groupPosition + "---child=" + childPosition + "---" + item_list.get(groupPosition).get(childPosition), Toast.LENGTH_SHORT).show();

                return false;
            }
        });

        //只打开一个子视图
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                int count = expandableListView.getExpandableListAdapter().getGroupCount();
                for (int i = 0; i < count; i++) {
                    if (i != groupPosition){
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });


    }


    class MyExpandableListViewAdapter  extends BaseExpandableListAdapter{

        private Context context;

        public MyExpandableListViewAdapter(Context context){
            this.context = context;
        }

        //获取组的个数
        @Override
        public int getGroupCount() {
            return group_list.size();
        }

        //获取指定组中的子元素个数
        @Override
        public int getChildrenCount(int groupPosition) {
            return item_list.get(groupPosition).size();
        }

        //获取指定组中的数据
        @Override
        public Object getGroup(int groupPosition) {
            return group_list.get(groupPosition);
        }

        //获取指定元素子元素的数据
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return item_list.get(groupPosition).get(childPosition);
        }

        //获取指定组的ID
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        //获取指定组中指定子元素的ID
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        //组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。
        @Override
        public boolean hasStableIds() {
            return true;
        }

        /**
         *
         * 获取显示指定组的视图对象
         *
         * @param groupPosition 组位置
         * @param isExpanded 该组是展开状态还是伸缩状态
         * @param convertView 重用已有的视图对象
         * @param parent 返回的视图对象始终依附于的视图组
         * @return
         * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View,
         *      android.view.ViewGroup)
         */
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            GroupHolder groupHolder = null;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.grouplayout,null);
                groupHolder = new GroupHolder();
                groupHolder.button = convertView.findViewById(R.id.button);
                groupHolder.textView = convertView.findViewById(R.id.groupTextView);
                convertView.setTag(groupHolder);
            }else{
                groupHolder = (GroupHolder) convertView.getTag();
            }if (!isExpanded){
                groupHolder.button.setText("没有展开");
            }else{
                groupHolder.button.setText("已经展开");
            }
            groupHolder.textView.setText(group_list.get(groupPosition));
            return convertView;
        }



        /**
         *
         * 获取一个视图对象，显示指定组中的指定子元素数据。
         *
         * @param groupPosition 组位置
         * @param childPosition 子元素位置
         * @param isLastChild 子元素是否处于组中的最后一个
         * @param convertView 重用已有的视图(View)对象
         * @param parent 返回的视图(View)对象始终依附于的视图组
         * @return
         * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
         *      android.view.ViewGroup)
         */
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            ItemHolder itemHolder = null;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.itemlayout,null);
                itemHolder = new ItemHolder();
                itemHolder.listView = convertView.findViewById(R.id.listView);
                convertView.setTag(itemHolder);
            }else{
                itemHolder = (ItemHolder) convertView.getTag();
            }

           itemHolder.listView.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,item_list.get(groupPosition)));

            return convertView;
        }


        //是否选中指定位置上的子元素。
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }

        class GroupHolder{
            TextView textView;
            TextView button;
        }

        class ItemHolder{
            ListView listView;
        }
    }




}

